package com.day0426.sys;/*
 * @创建者：黄苟苟
 * @创建时间：2021/4/26   10:43
 * @Description描述
 **/

import com.day0426.door.Computer;
import com.day0426.door.Door;
import com.day0426.door.InputEquip;
import com.day0426.user.Admin;

import java.util.logging.Logger;

public class ControlSys {
    private Door door;
    private Computer computer;
    private InputEquip inputEquip;
    private int ring;
    private int openSign;
    private Logger logger;


    public ControlSys() {
        this.door = new Door();
        this.computer = new Computer();
        this.inputEquip = new InputEquip();
        this.ring = 0;
        this.openSign = 0;
        this.logger = Logger.getLogger(Admin.class.getName());

        System.out.println("门禁系统的使用方法\n" +
                "1.模拟输入密码：以“pa”开头，后跟密码" +
                "2.模拟刷卡：以“ca”开头，后跟卡号\n" +
                "3.模拟取指纹：以“fi”开头，后跟表示指纹的字符串\n" +
                "4.模拟管理员按下开门按钮：输入“y”");
        System.out.println("***********************************");
        logger.info("门禁系统启动");
    }

    /*
     * 门禁系统工作：检查设备状态
     *
     * */
    public void work() {
        /*检查输入设备的输入缓存*/
        if (inputEquip.getInput() != null) {
            if (inputEquip.getInput() .equals("exit")) {
                logger.info("门禁系统关闭");
                System.exit(0);
            }
            if (computer.validate(inputEquip.getInput())) {
                door.open();
                logger.info("<身份验证成功，" + inputEquip.getInput() + "通过电子门>");
                inputEquip.setInput(null);
            } else {
                logger.info("身份验证失败");
            }
        }
        /*检查开门信号*/
        if (this.getOpenSign()==1){
            door.open();
            logger.info("管理员开启了电子门");
            this.setOpenSign(0);
        }
        if (door.getState().equals(Door.OPEN)){
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                logger.info(e.getMessage());
            }
            door.close();
            logger.info("电子门关闭");

        }
    }

    public void ring(){
        this.ring=1;
    }


    public InputEquip getInputEquip() {
        return inputEquip;
    }

    public void setInputEquip(InputEquip inputEquip) {
        this.inputEquip = inputEquip;
    }

    public int getRing() {
        return ring;
    }

    public void setRing(int ring) {
        this.ring = ring;
    }

    public int getOpenSign() {
        return openSign;
    }

    public void setOpenSign(int openSign) {
        this.openSign = openSign;
    }

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public Door getDoor() {
        return door;
    }

    public void setDoor(Door door) {
        this.door = door;
    }

    public Computer getComputer() {
        return computer;
    }

    public void setComputer(Computer computer) {
        this.computer = computer;
    }
}
